<template>
  <div class="todoapp">
    <TodoHeader @getname="addList" :arr="list"></TodoHeader>
    <TodoMain :arr="showArr" @del="delFn"></TodoMain>
    <TodoFooter
      :arr="showArr"
      @changeType="changeFn"
      @clearType="clearFn"
    ></TodoFooter>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  data() {
    return {
      cons: "all",
      list: JSON.parse(localStorage.getItem("todolist")) || [],
    };
  },

  methods: {
    addList(thename) {
      this.list.push({
        id:
          this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name: thename,
        isDone: false,
      });
    },
    delFn(index) {
      this.list.splice(index, 1);
    },
    changeFn(conStr) {
      this.cons = conStr;
    },
    clearFn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },

  computed: {
    showArr() {
      if (this.cons === "ok") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.cons === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },

  watch: {
    list: {
      handler(newVal) {
        localStorage.setItem("todolist", JSON.stringify(newVal));
      },
      deep: true,
    },
  },

  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>

<style>
</style>